package com.ikun.yummy.activitys.details;

import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.ikun.yummy.R;
import com.ikun.yummy.databinding.ActivityDetailBinding;

public class DetailActivity extends AppCompatActivity {
    private DetailVM vm;
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //为了方便解耦

        //通过DataBindingUtil的setContentView把布局实列化成为一个类binding
        ActivityDetailBinding binding= DataBindingUtil.setContentView(this, R.layout.activity_detail);
        //初始化一个ViewModel，拿到了代码以及模块的持有者
        vm=new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(DetailVM.class);
        //调用布局里的方法
        binding.setVm(vm);
        //给布局以生命周期
        binding.setLifecycleOwner(this);

        vm.setBinding(binding,this,getIntent().getStringExtra("cookid"));


        //返回箭头
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}